package com.zerui.manager.app.deviceTouch.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.manager.app.deviceTouch.convertor.DeviceTouchVoConvert;
import com.zerui.manager.device.domainservice.DeviceRepository;
import com.zerui.manager.deviceTouch.DeviceTouch;
import com.zerui.manager.deviceTouch.domainservice.DeviceTouchRepository;
import com.zerui.manager.devicegroup.DeviceGroup;
import com.zerui.manager.devicegroup.domainservice.DeviceGroupRepository;
import com.zerui.manager.web.cmd.DeviceTouchQryCmd;
import com.zerui.manager.web.vo.DeviceTouchPageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-23 16:29
 */
@Component
@CatchAndLog
public class DeviceTouchQryCmdExe {

    @Resource
    private DeviceTouchRepository deviceTouchRepository;
    @Resource
    private DeviceGroupRepository deviceGroupRepository;
    @Resource
    private DeviceRepository deviceRepository;

    public List<DeviceTouch> selectTouchDevicesByFrom(Integer deviceFrom) {
        return deviceTouchRepository.selectTouchDevicesByFrom(deviceFrom);
    }

    public Page<DeviceTouchPageVO> getPageDeviceList(DeviceTouchQryCmd qryCmd) {
        Set<String> deviceIds = null;
        if (ObjectUtils.isNotEmpty(qryCmd.getOrgId())) {
            DeviceGroup deviceGroup = deviceGroupRepository.selectById(Long.parseLong(qryCmd.getOrgId()));
            if (ObjectUtils.isNotEmpty(deviceGroup)) {
                deviceIds = deviceGroup.getDeviceIds();
                if (!"根节点".equals(deviceGroup.getOrgName()) && CollectionUtils.isEmpty(deviceIds)) {
                    return Page.of(
                            qryCmd.getPageSize(),
                            qryCmd.getPageIndex());
                }
                qryCmd.setDeviceIds(deviceIds.stream().map(Long::parseLong).
                        collect(Collectors.toList()));
            }
        }

        Page<DeviceTouch> deviceTouchesPage = deviceTouchRepository.getPageDeviceList(qryCmd);
        return DeviceTouchVoConvert.INSTANCE.covertDeviceTouchToPageVO(deviceTouchesPage);
    }

    public Page<DeviceTouchPageVO> getPageDeviceList2(DeviceTouchQryCmd qryCmd) {
        Set<String> deviceIds = null;
        if (ObjectUtils.isNotEmpty(qryCmd.getOrgId())) {
            DeviceGroup deviceGroup = deviceGroupRepository.selectById(Long.parseLong(qryCmd.getOrgId()));
            if (ObjectUtils.isNotEmpty(deviceGroup)) {
                deviceIds = deviceGroup.getDeviceIds();
                if (!"根节点".equals(deviceGroup.getOrgName()) && CollectionUtils.isEmpty(deviceIds)) {
                    return Page.of(
                            qryCmd.getPageSize(),
                            qryCmd.getPageIndex());
                }
                qryCmd.setDeviceIds(deviceIds.stream().map(Long::parseLong).
                        collect(Collectors.toList()));
            }
        }

        return deviceTouchRepository.getPageDeviceList2(qryCmd);
    }

    public List<DeviceInfoFeignVO> getAccessControlDeviceList(DeviceTouchQryCmd qryCmd) {
        Set<String> deviceIds = null;
        if (ObjectUtils.isNotEmpty(qryCmd.getOrgId())) {
            DeviceGroup deviceGroup = deviceGroupRepository.selectById(Long.parseLong(qryCmd.getOrgId()));
            if (ObjectUtils.isNotEmpty(deviceGroup)) {
                deviceIds = deviceGroup.getDeviceIds();
                if (!"根节点".equals(deviceGroup.getOrgName()) && CollectionUtils.isEmpty(deviceIds)) {
                    return Collections.emptyList();
                }
                qryCmd.setDeviceIds(deviceIds.stream().map(Long::parseLong).
                        collect(Collectors.toList()));
            }
        }
        return deviceTouchRepository.getAccessControlDeviceList(qryCmd);
    }

    public DeviceTouchPageVO getDeviceByDeviceCode(String deviceCode) {
        return deviceTouchRepository.getDeviceByDeviceCode(deviceCode);
    }
}
